/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.haui.helper;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

/**
 *
 * @author Haubrock
 */

/***
 * Generic class for easy serialization to xml.
 * @author Haubrock
 * @param <t> Type of Objects to de-/serialize
 */
public class Object2XML<t> {
    
    /***
     * Serializes the given Object to an xml file.
     * @param xmlFileLoc the Filename
     * @param object2Ser the Object 
     * @throws FileNotFoundException 
     */
    public void serialize(String xmlFileLoc,t object2Ser) throws FileNotFoundException
    {
        FileOutputStream os=new FileOutputStream(xmlFileLoc);
        XMLEncoder xml=new XMLEncoder(os);
        xml.writeObject(object2Ser);
        xml.close();
    }
    /***
     * Deserializes the given xml file via reflections to an object 
     * @param xmlFileLoc the Filename
     * @return 
     * @throws FileNotFoundException 
     */
    public t deserialize(String xmlFileLoc) throws FileNotFoundException
    {
        FileInputStream in=new FileInputStream(xmlFileLoc);
        XMLDecoder xml=new XMLDecoder(in);
        return (t)xml.readObject();
    }
}
